<template>
  <view class="m-32">
    <view class="address-card mt-24" v-for="item in dataList" :key="item.id">
      <view class="address-top">
        <view>
          <text class="color-[#666] text-28 mr-28">{{ item.contact }}</text>
          <text class="color-[#666] text-28">{{ item.phone }}</text>
        </view>
        <view class="color-333 font-32 fw-400">
          {{ item.address }} {{ item.addressDetail }}
        </view>
      </view>
      <view class="u-border-top">
        <view class="address-bottom">
          <view class="flex justify-between">
            <view>
              <u-radio-group
                v-model="item.isDefault"
                @change="setDefault($event, item.id)">
                <u-radio
                  active-color="#ff8f44"
                  label="设为默认"
                  :name="1"></u-radio>
              </u-radio-group>
            </view>
            <view>
              <text
                class="mr-40 color-[#333] text-28 fw-400"
                @click="editAddress(item.id)"
                >编辑</text
              >
              <text
                class="color-[#333] text-28 fw-400"
                @click="deleteAddress(item.id)"
                >删除</text
              >
            </view>
          </view>
        </view>
      </view>
    </view>
    <view class="text-center color-[#666] mt-32 text-28">
      {{ isDone ? "没有数据了~" : "正在加载..." }}
    </view>
    <view style="margin-top: 272rpx">
      <u-button
        type="primary"
        color="#FF8F44"
        @click="$u.route('/pagesA/address/add')"
        :custom-style="{ height: '96rpx' }"
        >新增地址</u-button
      >
    </view>
  </view>
</template>

<script setup>
const dataList = ref([
  {
    contact: "张有志",
    phone: "17353678794",
    address: "山东省潍坊市寒亭区",
    addressDetail: "第一加速器2楼",
  },
]);
// 设置默认地址
const setDefault = async (name, id) => {
  await dataList.value.forEach((item) => {
    if (item.id == id) {
      item.isDefault = 1;
    } else {
      item.isDefault = 0;
    }
  });
  // await setDefaultAddressApi(id);
};
// 删除地址
const deleteAddress = (id) => {
  uni.$sc.showConfirm("确定要删除该地址吗？").then((confirmRes) => {
    if (confirmRes.confirm) {
      // deleteAddressApi(id).then((res) => {
      //   if (res.success) {
      //     uni.$u.toast("删除成功！");
      //     uni.$u.sleep(800).then(() => {
      //       dataList.value = [];
      //       isDone.value = false;
      //       pageNo.value = 1;
      //       loadData();
      //     });
      //   }
      // });
    }
  });
};
const editAddress = (id) => {
  uni.$u.route({
    url: "/pagesA/address/add",
    params: {
      id,
      isEdit: true,
    },
  });
};
const test = () => {
  dataList.value.push({
    contact: "宋小丽",
    phone: "18265626425",
    address: "山东省潍坊市寒亭区",
    addressDetail: "第一加速器2楼",
  });
};
defineExpose({
  test,
});
onShow(() => {
  // isDone.value = false;
  // pageNo.value = 1;
  // dataList.value = [];
  // loadData();
});
</script>
<style lang="scss" scoped>
.address-card {
  background-color: #fff;
  .address-top {
    padding: 40rpx 32rpx;
  }
  .address-bottom {
    padding: 26rpx 30rpx;
  }
}
</style>
